package com.ruoyi.web.controller.set;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.SetModel;
import com.ruoyi.system.domain.base.MesScanKeysRelation;
import com.ruoyi.system.domain.base.MesScanKeysRelationDTO;
import com.ruoyi.system.service.ISetModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;

/**
 * 车型Controller
 *
 * @author ruoyi
 * @date 2022-11-21
 */
@RestController
@Api(tags = "产品信息api")
@RequestMapping("/set/model")
public class SetModelController extends BaseController
{
    @Autowired
    private ISetModelService setModelService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询车型列表
     */
    @PreAuthorize("@ss.hasAnyPermi('set:model:list')")
    @ApiOperation(value = "查询车型列表")
    @GetMapping("/list")
    public TableDataInfo list(@ApiParam(value = "车型参数", required = false) SetModel setModel)
    {
        startPage();
        List<SetModel> list = setModelService.selectSetModelList(setModel);
        return getDataTable(list);
    }

    /**
     * 根据产品id查询关重件扫描验证关系
     */
    @ApiOperation(value = "根据产品id查询关重件扫描验证关系")
    @GetMapping("/getscanKeysRelationFindId/{id}")
    public TableDataInfo getscanKeysRelationFindId(@PathVariable Integer id)
    {
        startPage();
        List<MesScanKeysRelationDTO> list = setModelService.getscanKeysRelationFindId(id);
        return getDataTable(list);
    }

    /**
     * 查询车型下拉接口
     */
//    @PreAuthorize("@ss.hasPermi('set:model:interface')")
    @ApiOperation(value = "查询产品下拉接口")
    @GetMapping("/interface")
    public AjaxResult interfaces(@ApiParam(value = "车型参数", required = false) SetModel setModel)
    {

        List<SetModel> list = setModelService.selectSetModelList(setModel);
        return AjaxResult.success(list);
    }

    /**
     * 导出车型列表
     */
    @PreAuthorize("@ss.hasPermi('set:model:export')")
    @Log(title = "车型", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "导出车型列表")
    @GetMapping("/export")
    public AjaxResult export(HttpServletResponse response, @ApiParam(value = "车型参数", required = false) SetModel setModel)
    {
        LambdaQueryWrapper<SetModel> lqw = new LambdaQueryWrapper<SetModel>(setModel);
        List<SetModel> list = setModelService.selectSetModelList(setModel);
        ExcelUtil<SetModel> util = new ExcelUtil<SetModel>(SetModel.class);
        return util.exportExcel( list, "车型数据");
    }

    /**
     * 导入车型列表
     */
    @Log(title = "车型", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('set:model:import')")
    @ApiOperation(value = "导入车型列表")
    @PostMapping("/importData")
    public AjaxResult importData(@ApiParam(value = "导入文件", required = true) MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SetModel> util = new ExcelUtil<SetModel>(SetModel.class);
        List<SetModel> modelList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = setModelService.importModel(modelList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 导入模板
     */
    @ApiOperation(value = "导入模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        List<SetModel> list = setModelService.selectSetModelList(new SetModel());
        ExcelUtil<SetModel> util = new ExcelUtil<SetModel>(SetModel.class);
        return util.importTemplateExcel(list,"车型数据");
    }

    /**
     * 获取车型详细信息
     */
    @PreAuthorize("@ss.hasPermi('set:model:query')")
    @ApiOperation(value = "获取车型详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam(value = "车型id", required = true) @PathVariable("id") Long id)
    {
        return AjaxResult.success(setModelService.selectSetModelById(id));
    }

    /**
     * 新增车型
     */
    @PreAuthorize("@ss.hasPermi('set:model:add')")
    @Log(title = "车型", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增车型")
    @PostMapping
    public AjaxResult add(@ApiParam(value = "车型实体类", required = true) @RequestBody SetModel setModel)
    {
//        if (UserConstants.NOT_UNIQUE.equals(setModelService.checkCodeUnique(setModel)))
//        {
//            return AjaxResult.error("新增车型'" + setModel.getModel() + "'失败，车型编码已存在");
//        }
//        else if (UserConstants.NOT_UNIQUE.equals(setModelService.checkNameUnique(setModel)))
//        {
//            return AjaxResult.error("新增车型'" + setModel.getModel() + "'失败，车型名称已存在");
//        }
        return toAjax(setModelService.insertSetModel(setModel));
    }

    /**
     * 修改车型
     */
    @PreAuthorize("@ss.hasPermi('set:model:edit')")
    @Log(title = "车型", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改车型")
    @PutMapping
    public AjaxResult edit(@ApiParam(value = "车型实体类", required = true) @RequestBody SetModel setModel)
    {
        if (UserConstants.NOT_UNIQUE.equals(setModelService.checkCodeUnique(setModel)))
        {
            return AjaxResult.error("修改车型'" + setModel.getModel() + "'失败，车型编码已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(setModelService.checkNameUnique(setModel)))
        {
            return AjaxResult.error("修改车型'" + setModel.getModel() + "'失败，车型名称已存在");
        }
        return toAjax(setModelService.updateSetModel(setModel));
    }

    /**
     * 删除车型
     */
    @PreAuthorize("@ss.hasPermi('set:model:remove')")
    @Log(title = "车型", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除车型")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam(value = "车型id数组", required = true) @PathVariable Long[] ids)
    {
        return toAjax(setModelService.deleteSetModelByIds(ids));
    }
}
